package com.jishuo.nontax.bfc.service;

import com.jishuo.base.bean.BaseResult;
import com.jishuo.base.bean.ErrorCodeEnum;
import com.jishuo.base.config.NonTaxConfig;
import com.jishuo.base.dao.UserMapper;
import com.jishuo.base.service.SequenceService;
import com.jishuo.nontax.bean.vo.BankPosVO;
import com.jishuo.nontax.bfc.dao.BankposMapper;
import com.jishuo.nontax.bfc.dao.SumposMapper;
import com.jishuo.nontax.bfc.pzk.PzkService;
import com.jishuo.nontax.bfc.tlq.MessageFactory;
import com.jishuo.nontax.bfc.tlq.MessageService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @auther dinggaofei
 * @Date 2022/12/21 10:36
 */
@Service
public class BankposService {

    private static Logger logger = LoggerFactory.getLogger(BankposService.class);

    @Resource
    private BankposMapper bankposMapper;
    @Resource
    private SequenceService sequenceService;
    @Resource
    private MessageService messageService;
    @Resource
    private NonTaxConfig nonTaxConfig;
    @Resource
    private UserMapper userMapper;
    @Resource
    private SumposMapper sumposMapper;

    /**
     * @param bankpos pos对象
     * @auther dinggaofei
     * @Date 2022/12/21 11:00
     * @description 添加修改  记录银行POS记录
     */
    public BaseResult setBankPos(BankPosVO bankpos) {
        try {
            bankpos.setSign(1);
            bankpos.setPurpose(4);
            if (bankposMapper.findBankPosCount(bankpos.getBankcheckid(), bankpos.getAccountnum(), bankpos.getAccountdate()) > 0) {
                if (bankposMapper.updateBankPos(bankpos) > 0) {
                    return new BaseResult();
                }
            } else {
                if (bankposMapper.insertBankPos(bankpos) > 0) {
                    return new BaseResult();
                }
            }
            return new BaseResult(ErrorCodeEnum.DB_ERR);
        } catch (Exception e) {
            logger.info("处理失败：" + e.toString());
            return new BaseResult(ErrorCodeEnum.UNKNOWN_ERR);
        }
    }

    /**
     * @param
     * @auther dinggaofei
     * @Date 2022/12/21 11:35
     * @description 向财政发送pos记录，并修改发送状态
     */
    public BaseResult sendBankPos(String accountcode, String accountdate) {

        try {
            accountdate = accountdate.substring(0, 10);

            ArrayList<BankPosVO> list =bankposMapper.findSendBankPos(accountcode, accountdate);
            if (list != null && list.size() > 0) {
                //状态修改为已发送
                bankposMapper.updateSendBankPos(accountcode,accountdate);
                Object setBankPosDetail = MessageFactory.getSetBankPosDetail(list);
                //发送到财政
                messageService.sendMessage("SETBANKPOSDETAIL", sequenceService.getNextSeq("ELEMESSAGESEQ"), setBankPosDetail, "2.0");
                // 写凭证
                if (nonTaxConfig.IS_Pzk) {
                    PzkService.sendPzk("SETBANKPOSDETAILPZK", setBankPosDetail);
                }
            }
            return new BaseResult();
        } catch (Exception e) {
            logger.info("处理失败：" + e.toString());
            return new BaseResult(ErrorCodeEnum.UNKNOWN_ERR);
        }
    }

    public BaseResult sendDeleteBankPos(String accountnum, String accountdate, String types, String usercode, String userpwd){
        if(userMapper.getUserCount(userpwd, usercode)>0){
            if(accountnum!=null&&accountnum!=""){
                if(types!=null && types.trim().equals("1")){
                    sendBankPos(accountnum,accountdate);
                }else{
                    sumposMapper.deletesumpos(accountnum,accountdate);
                }
                return new BaseResult();
            }
        }
        return new BaseResult();
    }

}
